﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

using System.Text.RegularExpressions;
using System.Collections;
namespace AndroidEditor.WForm
{
    /// <summary>
    /// 
    /// </summary>
    /// <remarks></remarks>
    public partial class ProjectProperties : Form
    {
        /// <summary>
        /// 
        /// </summary>
        private Hashtable flagTable = new Hashtable();
        /// <summary>
        /// 
        /// </summary>
        private Hashtable libsTable = new Hashtable();
        /// <summary>
        /// 
        /// </summary>
        private List<string> DependHeaderDirectory = new List<string>();
        /// <summary>
        /// 
        /// </summary>
        private List<string> DependLibsDirectory = new List<string>();
        /// <summary>
        /// 
        /// </summary>
        private List<ProjectSetting> Dp = new List<ProjectSetting>();
        /// <summary>
        /// 
        /// </summary>
        private bool isJs = true;
        /// <summary>
        /// 
        /// </summary>
        private string VBscript = @" ";
        /// <summary>
        /// 
        /// </summary>
        private string JScript = @"//javascript Start from main
function  main()
{
}";
        /// <summary>
        /// Initializes a new instance of the <see cref="ProjectProperties"/> class.
        /// </summary>
        /// <remarks></remarks>
        public ProjectProperties()
        {
            InitializeComponent();

        }





        /// <summary>
        /// Gets the debug flag.
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        public bool GetDebugFlag()
        {
            return checkBox1.Checked;
        }

        /// <summary>
        /// Handles the Click event of the button1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void button1_Click(object sender, EventArgs e)
        {


            DialogResult = DialogResult.OK;

        }




        /// <summary>
        /// Handles the Click event of the button2 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }




        /// <summary>
        /// Checks the create or set hash table.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <remarks></remarks>
        private void CheckCreateOrSetHashTable(string key, string value)
        {
            if (flagTable.ContainsKey(key))
            {
                flagTable[key] = value;
            }
            else
            {
                flagTable.Add(key, value);
            }
        }

        /// <summary>
        /// Checks the create or set hash table libs.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <remarks></remarks>
        private void CheckCreateOrSetHashTableLibs(string key, string value)
        {
            if (libsTable.ContainsKey(key))
            {
                libsTable[key] = value;
            }
            else
            {
                libsTable.Add(key, value);
            }
        }
        /// <summary>
        /// Handles the CheckedChanged event of the checkBox7 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void checkBox7_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox7.Checked)
            {
                CheckCreateOrSetHashTable("ExceptionFlag", "-fexceptions");
            }
            else
            {
                CheckCreateOrSetHashTable("ExceptionFlag", "");
            }
        }

        /// <summary>
        /// Handles the 1 event of the checkBox8_CheckedChanged control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void checkBox8_CheckedChanged_1(object sender, EventArgs e)
        {
            if (checkBox8.Checked)
            {
                CheckCreateOrSetHashTable("RTTIFlag", "-frtti");
            }
            else
            {
                CheckCreateOrSetHashTable("RTTIFlag", "");
            }
        }

        /// <summary>
        /// Handles the Click event of the button5 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void button5_Click(object sender, EventArgs e)
        {
            List<int> deleteline = new List<int>();
            for (int i = 0; i < listBox1.Items.Count; i++)
            {
                string path = listBox1.Items[i].ToString();
                path = path.Replace("/", @"\");

                if (!Directory.Exists(path))
                {
                    deleteline.Add(i);
                }

            }
            deleteline.Reverse();
            foreach (int dline in deleteline)
            {
                listBox1.Items.RemoveAt(dline);
            }
        }

        /// <summary>
        /// Handles the Click event of the button3 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void button3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fdialog = new FolderBrowserDialog();

            fdialog.SelectedPath = @"c:\";
            if (fdialog.ShowDialog() == DialogResult.OK)
            {
                List<int> deleteline = new List<int>();
                for (int i = 0; i < listBox1.Items.Count; i++)
                {
                    string path = listBox1.Items[i].ToString();
                    path = path.Replace("/", @"\").Trim();
                    if (fdialog.SelectedPath.Trim() == path)
                    {
                        return;
                    }


                }

                listBox1.Items.Add(fdialog.SelectedPath.Replace(@"\", "/"));
            }
            fdialog.Dispose();

        }

        /// <summary>
        /// Handles the Click event of the button4 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void button4_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex > -1)
            {
                listBox1.Items.RemoveAt(listBox1.SelectedIndex);
            }
        }



        /// <summary>
        /// Gets the DHD.
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        public List<string> GetDHD()
        {
            List<string> dhd = new List<string>();
            for (int i = 0; i < listBox1.Items.Count; i++)
            {
                string path = listBox1.Items[i].ToString();
                dhd.Add(path);

            }
            return dhd;
        }

        /// <summary>
        /// Sets the depend header directory.
        /// </summary>
        /// <param name="DependHeaderDirectory">The depend header directory.</param>
        /// <remarks></remarks>
        public void SetDependHeaderDirectory(List<string> DependHeaderDirectory)
        {
            this.DependHeaderDirectory = DependHeaderDirectory;
        }

        /// <summary>
        /// Handles the Load event of the ProjectProperties control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void ProjectProperties_Load(object sender, EventArgs e)
        {
            foreach (string Item in DependHeaderDirectory)
            {
                listBox1.Items.Add(Item);
            }
            foreach (string Item in DependLibsDirectory)
            {
                listBox2.Items.Add(Item);
            }
            foreach (ProjectSetting Item in Dp)
            {
                checkedListBox1.Items.Add(Item.getProjectName());


            }
            toolTip1.AutoPopDelay = 5000;
            toolTip1.AutomaticDelay = 5000;
            toolTip1.ReshowDelay = 500;
            toolTip1.ShowAlways = true;

            toolTip1.SetToolTip(textBox1, "调置工程名字，动态或静态库将会自动在前边加上lib!");
            if (isJs)
            {

                radioButton2.Checked = true;
            }
            else
            {
                radioButton1.Checked = true;
            }
        }



        /// <summary>
        /// Sets the depend project.
        /// </summary>
        /// <param name="Dp">The dp.</param>
        /// <remarks></remarks>
        internal void SetDependProject(List<ProjectSetting> Dp)
        {
            this.Dp = Dp;
        }



        /// <summary>
        /// Handles the ItemCheck event of the checkedListBox1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.ItemCheckEventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            try
            {
                if (e.NewValue == CheckState.Checked)
                {
                    string SelectProject = checkedListBox1.Items[e.Index].ToString();
                    foreach (ProjectSetting Item in Dp)
                    {
                        if (SelectProject == Item.getProjectName())
                        {

                        }
                    }
                }

            }
            catch (Exception ex)
            {
            }
        }

        /// <summary>
        /// Handles the Click event of the button8 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void button8_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fdialog = new FolderBrowserDialog();


            if (fdialog.ShowDialog() == DialogResult.OK)
            {
                List<int> deleteline = new List<int>();
                for (int i = 0; i < listBox2.Items.Count; i++)
                {
                    string path = listBox2.Items[i].ToString();
                    path = path.Replace("/", @"\").Trim();
                    if (fdialog.SelectedPath.Trim() == path)
                    {
                        return;
                    }


                }

                listBox2.Items.Add(fdialog.SelectedPath.Replace(@"\", "/"));
            }
            fdialog.Dispose();
        }

        /// <summary>
        /// Handles the Click event of the button7 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void button7_Click(object sender, EventArgs e)
        {
            if (listBox2.SelectedIndex > -1)
            {
                listBox2.Items.RemoveAt(listBox2.SelectedIndex);
            }
        }

        /// <summary>
        /// Handles the Click event of the button6 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void button6_Click(object sender, EventArgs e)
        {
            List<int> deleteline = new List<int>();
            for (int i = 0; i < listBox2.Items.Count; i++)
            {
                string path = listBox2.Items[i].ToString();
                path = path.Replace("/", @"\");

                if (!Directory.Exists(path))
                {
                    deleteline.Add(i);
                }

            }
            deleteline.Reverse();
            foreach (int dline in deleteline)
            {
                listBox2.Items.RemoveAt(dline);
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the checkBox4 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Handles the CheckedChanged event of the checkBox5 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void checkBox5_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox7.Checked)
            {
                CheckCreateOrSetHashTable("ExceptionFlag", "-fno-exceptions");
            }
            else
            {
                CheckCreateOrSetHashTable("ExceptionFlag", "");
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the checkBox6 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void checkBox6_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox7.Checked)
            {
                CheckCreateOrSetHashTable("ExceptionFlag", "-fno-exceptions");
            }
            else
            {
                CheckCreateOrSetHashTable("ExceptionFlag", "");
            }
        }



        /// <summary>
        /// Gets the D libs.
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        public List<string> GetDLibs()
        {
            List<string> dhd = new List<string>();
            for (int i = 0; i < listBox2.Items.Count; i++)
            {
                string path = listBox2.Items[i].ToString();
                dhd.Add(path);

            }
            return dhd;
        }

        /// <summary>
        /// Sets the depend libs directory.
        /// </summary>
        /// <param name="list">The list.</param>
        /// <remarks></remarks>
        public void SetDependLibsDirectory(List<string> list)
        {
            DependLibsDirectory = list;
        }

        /// <summary>
        /// Handles the CheckedChanged event of the checkBox1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                CheckCreateOrSetHashTable("DebugFlag", "-g");
            }
            else
            {
                CheckCreateOrSetHashTable("DebugFlag", "");
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the checkBox2 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked)
            {
                CheckCreateOrSetHashTable("WarningAll", "-Wall");
            }
            else
            {
                CheckCreateOrSetHashTable("WarningAll", "");
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the checkBox10 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void checkBox10_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox10.Checked)
            {
                CheckCreateOrSetHashTable("Errorall", "-Werror");
            }
            else
            {
                CheckCreateOrSetHashTable("Errorall", "");
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the checkBox11 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void checkBox11_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox10.Checked)
            {
                CheckCreateOrSetHashTable("Optinzme", "-O3");
            }
            else
            {
                CheckCreateOrSetHashTable("Optinzme", "");
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the checkBox13 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void checkBox13_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox13.Checked)
            {
                CheckCreateOrSetHashTable("pipe", "-pipe");
            }
            else
            {
                CheckCreateOrSetHashTable("pipe", "");
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the checkBox9 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void checkBox9_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox9.Checked)
            {
                CheckCreateOrSetHashTableLibs("z", "-lz");
            }
            else
            {
                CheckCreateOrSetHashTableLibs("z", "");
            }
        }

        /// <summary>
        /// Handles the 1 event of the checkBox5_CheckedChanged control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void checkBox5_CheckedChanged_1(object sender, EventArgs e)
        {
            if (checkBox5.Checked)
            {
                CheckCreateOrSetHashTableLibs("jni", "-ljnigraphics");
            }
            else
            {
                CheckCreateOrSetHashTableLibs("jni", "");
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the checkBox3 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox3.Checked)
            {
                CheckCreateOrSetHashTableLibs("log", "-llog");
            }
            else
            {
                CheckCreateOrSetHashTableLibs("log", "");
            }
        }

        /// <summary>
        /// Handles the 1 event of the checkBox6_CheckedChanged control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void checkBox6_CheckedChanged_1(object sender, EventArgs e)
        {

            if (checkBox6.Checked)
            {
                CheckCreateOrSetHashTableLibs("dl", "-ldl");
            }
            else
            {
                CheckCreateOrSetHashTableLibs("dl", "");
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the radioButton1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            textBox2.Text = VBscript;
            isJs = false;

        }

        /// <summary>
        /// Handles the CheckedChanged event of the radioButton2 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            isJs = true;

            textBox2.Text = JScript;
        }

        /// <summary>
        /// Handles the TextChanged event of the textBox2 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {

                VBscript = textBox2.Text;

            }
            else
            {
                JScript = textBox2.Text;
            }
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the checkedListBox1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


        /// <summary>
        /// Gets the is js.
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        public bool GetIsJs()
        {
            return isJs;
        }

        /// <summary>
        /// Gets the VB.
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        public string GetVB()
        {
            return VBscript;
        }
        /// <summary>
        /// Gets the JS.
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        public string GetJS()
        {
            return JScript;
        }

        /// <summary>
        /// Sets the VB.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <remarks></remarks>
        internal void SetVB(string value)
        {
            VBscript = value;
        }

        /// <summary>
        /// Sets the JS.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <remarks></remarks>
        internal void SetJS(string value)
        {
            JScript = value;
        }

        /// <summary>
        /// Sets the is JS.
        /// </summary>
        /// <param name="value">if set to <c>true</c> [value].</param>
        /// <remarks></remarks>
        internal void SetIsJS(bool value)
        {
            isJs = value;
        }

        /// <summary>
        /// Handles the TextChanged event of the textBox3 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void textBox3_TextChanged(object sender, EventArgs e)
        {

            CheckCreateOrSetHashTable("UserFlags", textBox3.Text);

        }

        /// <summary>
        /// Handles the TextChanged event of the textBox4 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            CheckCreateOrSetHashTableLibs("UserLibs", textBox4.Text);
        }

        /// <summary>
        /// Gets the libs.
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        internal Hashtable GetLibs()
        {
            return libsTable;
        }

        /// <summary>
        /// Gets the flags.
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        internal Hashtable GetFlags()
        {
            return flagTable;
        }

        /// <summary>
        /// Sets the flags.
        /// </summary>
        /// <param name="hashtable">The hashtable.</param>
        /// <remarks></remarks>
        internal void SetFlags(Hashtable hashtable)
        {
            flagTable = hashtable;
        }

        /// <summary>
        /// Sets the libs.
        /// </summary>
        /// <param name="hashtable">The hashtable.</param>
        /// <remarks></remarks>
        internal void SetLibs(Hashtable hashtable)
        {
            libsTable = hashtable;
        }
    }
}
